package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.Employee;
import com.marktotop.mapper.EmployeeMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author marktotop
 * @since 2021-05-23
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 获取所员工(分页)
     * @param currentPage
     * @param size
     * @param employee
     * @param beginDateScope
     * @return
     */
    @Override
    public ResultBean getEmployeeByPage(Integer currentPage,
                                        Integer size,
                                        Employee employee,
                                        LocalDate[] beginDateScope) {
        // 开启分页
        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeByPage(page, employee, beginDateScope);
        if (null == employeeByPage){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取员工信息成功！")
                .data("total",employeeByPage.getTotal())
                .data("employees",employeeByPage.getRecords());
    }

    /**
     * 获取工号
     * @return
     */
    @Override
    public String maxWorkID() {
        List<Map<String, Object>> list = employeeMapper.selectMaps(new QueryWrapper<Employee>().select("max(workID)"));
        return String.format("%08d",Integer.parseInt(list.get(0).get("max(workID)").toString())+1);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public ResultBean addEmp(Employee employee) {
        //处理合同期限，保留两位小数。
        LocalDate beginContract = employee.getBeginContract();
        LocalDate endContract = employee.getEndContract();
        long days = beginContract.until(endContract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(days/365)));
        if (1 == employeeMapper.insert(employee)) {
            return ResultBean.ok().message("员工添加成功!");
        }
        return ResultBean.error().message("员工添加失败!");
    }

    /**
     * 获取所有员工工资账套
     *
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public ResultBean getEmployeeSalary(Integer currentPage, Integer size) {
        // 开启分页
        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> employeeByPage = employeeMapper.getEmployeeSalary(page);
        if (null == employeeByPage){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取员工信息成功！")
                .data("total",employeeByPage.getTotal())
                .data("employees",employeeByPage.getRecords());
    }

    /**
     * 获取所有数据导出excel
     * @param id
     */
    @Override
    public List<Employee> getEmployee(Integer id) {
       return employeeMapper.getEmployee(id);
    }
}
